<?php

declare (strict_types=1);

namespace App\Model;

/**
 * @property int $id
 * @property string $name
 * @property string $title
 * @property int $status
 * @property string $power
 */
class AuthGroup extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'auth_group';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'status' => 'integer'];

    public function ownMenu()
    {
        return $this->belongsToMany(AuthMenu::class, 'auth_group_menu_ship');
    }


    /**
     * 获取全部 管理组
     * @return array
     */
    public static function getAllList()
    {

        $list = self::orderByDesc('id')->get()->toArray();
        var_dump($list);

        return self::buildTree($list);


    }


    public static function buildTree($data, $parentId = 0)
    {
        $tree = [];

        foreach ($data as $item) {
            if ($item['pid'] == $parentId) {
                $children = self::buildTree($data, $item['id']);
                if ($children) {
                    $item['children'] = $children;
                }
                $tree[] = $item;
            }
        }

        return $tree;
    }
}